home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / GetPPPStatus / MoreFinderEvents.c < prev    next >
Encoding:
Text File  |  2000-09-28  |  14.7 KB  |  578 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        MoreFinderEvents.c
  3.  
  4.     Contains:    Functions to help you build and sending Apple events to the Finder.
  5.  
  6.     Written by: Andy Bachorski    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/22/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. //    Constant used to #undef PASCAL when not compiling a library
  24. #define    COMPILING_MORE_FINDER_EVENTS    true
  25.  
  26.  
  27. //    System includes
  28. #include <Aliases.h>
  29. #include <AppleScript.h>
  30. #include <Components.h>
  31. #include <Folders.h>
  32. #include <Icons.h>
  33. #include <OSA.h>
  34.  
  35.  
  36. //    C standard library includes
  37. #include <string.h>
  38.  
  39.  
  40. //    MoreFinderEvents includes
  41. #include "FinderRegistry.h"
  42. #include "AEHelpers.h"
  43.  
  44.  
  45. //    Export symbols in this header for shared libraries
  46. #pragma export on
  47. #include "MoreFinderEvents.h"
  48. #pragma export off
  49.  
  50. #define    COMPILING_MORE_FINDER_EVENTS    true
  51.  
  52.  
  53. const    Boolean        flagNotSet = -1;
  54.  
  55. static    Boolean        hasAppleEvents = flagNotSet;
  56. static    Boolean        finderCallsAEProcess = flagNotSet;
  57. static    Boolean        finderIsOSLCompliant = flagNotSet;
  58.  
  59.  
  60. // *****************************************************************************
  61.  
  62. pascal OSErr MFESetSelectionToNone( const AEIdleUPP idleProcUPP )
  63. {
  64.     OSErr        anErr = noErr;
  65.     
  66.     AppleEvent    theEvent = { typeNull, nil };
  67.     
  68.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  69.                                               kAECoreSuite, kAESetData, &theEvent );
  70.     if ( anErr == noErr )
  71.     {
  72.         AEDesc         propertyObject = { typeNull, nil };
  73.         AEDesc        containerObj = { typeNull, nil };    // start with the null (application) container
  74.             
  75.         anErr = AEHMakePropertyObject( pSelection, &containerObj, &propertyObject );
  76.         if ( anErr == noErr )
  77.         {
  78.             anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
  79.             AEDisposeDesc( &propertyObject );
  80.             if ( anErr == noErr )
  81.             {
  82.                 AEDescList        emptyList = { typeNull, nil };
  83.                 
  84.                 anErr = AECreateList( nil, 0, false, &emptyList );
  85.                 if ( anErr == noErr )
  86.                 {
  87.                     anErr = AEPutParamDesc( &theEvent, keyAEData, &emptyList );
  88.                     if ( anErr == noErr )
  89.                     {
  90.                         AppleEvent    theReply = { typeNull, nil };
  91.                         AESendMode    sendMode;
  92.                         
  93.                         if ( idleProcUPP == nil )
  94.                             sendMode = kAENoReply;
  95.                         else
  96.                             sendMode = kAEWaitReply;
  97.  
  98.                         anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  99.                                         kNoTimeOut, idleProcUPP, nil );
  100.                         
  101.                         AEDisposeDesc( &theEvent );
  102.                         
  103.                         if ( anErr == noErr && idleProcUPP != nil )
  104.                         {
  105.                             anErr =  AEHGetHandlerError( &theReply );
  106.                             
  107.                             AEDisposeDesc( &theReply );
  108.                         }
  109.                     }
  110.                 }
  111.             }
  112.         }
  113.     }
  114.     
  115.     return anErr;
  116.     
  117. } // MFESetSelectionToNone
  118.  
  119. // *****************************************************************************
  120.  
  121. pascal OSErr MFEChangeFolderViewNewSuite( const FSSpecPtr fssPtr,
  122.                                           const long viewStyle,
  123.                                           const AEIdleUPP idleProcUPP )
  124. {
  125.     OSErr        anErr = noErr;
  126.     
  127.     AppleEvent    theEvent = {typeNull, nil};
  128.     
  129.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  130.                                               kAECoreSuite, kAESetData, &theEvent );
  131.     if ( anErr == noErr )
  132.     {
  133.         AEDesc         folderObject = {typeNull, nil};
  134.         AEDesc        containerObj = { typeNull, nil };    // start with the null (application) container
  135.         
  136.         anErr = AEHMakeAliasObjectFromFSSpec( fssPtr, &containerObj, &folderObject );
  137.         if ( anErr == noErr )
  138.         {
  139.             AEDesc         propertyObject = {typeNull, nil};
  140.             
  141.             anErr = AEHMakePropertyObject( pView, &folderObject, &propertyObject );    // disposes of folderObject for us
  142.             if ( anErr == noErr )
  143.             {
  144.                 anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
  145.                 AEDisposeDesc( &propertyObject );
  146.                 if ( anErr == noErr )
  147.                 {
  148.                     anErr = AEPutParamPtr( &theEvent, keyAEData,
  149.                                            typeLongInteger, &viewStyle, sizeof(viewStyle) );
  150.                     if ( anErr == noErr )
  151.                     {
  152.                         AppleEvent    theReply = {typeNull, nil};
  153.                         AESendMode    sendMode;
  154.                         
  155.                         if ( idleProcUPP == nil )
  156.                             sendMode = kAENoReply;
  157.                         else
  158.                             sendMode = kAEWaitReply;
  159.  
  160.                         anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  161.                                         kNoTimeOut, idleProcUPP, nil );
  162.                         
  163.                         AEDisposeDesc( &theEvent );
  164.                         
  165.                         if ( anErr == noErr && idleProcUPP != nil )
  166.                         {
  167.                             anErr =  AEHGetHandlerError( &theReply );
  168.                             
  169.                             AEDisposeDesc( &theReply );
  170.                         }
  171.                     }
  172.                 }
  173.             }
  174.         }
  175.     }
  176.     
  177.     return anErr;
  178.     
  179. } // MFEChangeFolderView
  180.  
  181. // *****************************************************************************
  182.  
  183. pascal OSErr MFEChangeFolderViewOldSuite( const FSSpecPtr fssPtr,
  184.                                           const long viewStyle,
  185.                                           const AEIdleUPP idleProcUPP )
  186. {
  187.     OSErr        anErr = noErr;
  188.     
  189.     AppleEvent    theEvent = {typeNull, nil};
  190.     
  191.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  192.                                               kAEFinderEvents, kAEChangeView, &theEvent );
  193.     if ( anErr == noErr )
  194.     {
  195.         AEDesc    aliasObject = { typeNull, nil };    // start with the null (application) container
  196.         
  197.         anErr = AEHMakeAliasDescFromFSSpec( fssPtr, &aliasObject );
  198.         
  199.         if ( anErr == noErr )
  200.         {
  201.             anErr = AEPutParamDesc( &theEvent, keyDirectObject, &aliasObject );
  202.             AEDisposeDesc( &aliasObject );
  203.             
  204.             if ( anErr == noErr )
  205.             {
  206.                 anErr = AEPutParamPtr( &theEvent, keyMiscellaneous,
  207.                                        typeLongInteger, &viewStyle, sizeof( viewStyle ) );
  208.                 if ( anErr == noErr )
  209.                 {
  210.                     AppleEvent    theReply = {typeNull, nil};
  211.                     AESendMode    sendMode;
  212.                     
  213.                     if ( idleProcUPP == nil )
  214.                         sendMode = kAENoReply;
  215.                     else
  216.                         sendMode = kAEWaitReply;
  217.  
  218.                     anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  219.                                     kNoTimeOut, idleProcUPP, nil );
  220.                     
  221.                     AEDisposeDesc( &theEvent );
  222.                     
  223.                     if ( anErr == noErr && idleProcUPP != nil )
  224.                     {
  225.                         anErr =  AEHGetHandlerError( &theReply );
  226.                         
  227.                         AEDisposeDesc( &theReply );
  228.                     }
  229.                 }
  230.             }
  231.         }
  232.     }
  233.     
  234.     return anErr;
  235.     
  236. } // MFEChangeFolderViewOldSuite
  237.  
  238. // *****************************************************************************
  239.  
  240. pascal OSErr MFEChangeFolderView( const FSSpecPtr fssPtr,
  241.                                   const long viewStyle,
  242.                                   const AEIdleUPP idleProcUPP )
  243. {
  244.     OSErr    anErr = noErr;
  245.     
  246.     if ( FinderCallsAEProcess() )
  247.     {
  248.         if ( FinderIsOSLCompliant() )
  249.         {
  250.             anErr = MFEChangeFolderViewNewSuite( fssPtr, viewStyle, idleProcUPP );
  251.         }
  252.         else
  253.         {
  254.             anErr = MFEChangeFolderViewOldSuite( fssPtr, viewStyle, idleProcUPP );
  255.         }
  256.     }
  257.     else
  258.     {
  259.         anErr = errAEEventNotHandled;
  260.     }
  261.     
  262.     return anErr;
  263. }//end MFEChangeFolderView
  264.  
  265. // *****************************************************************************
  266.  
  267. pascal OSErr MFEAddCustomIconToItem( const FSSpecPtr fssPtr,
  268.                                      const Handle theIconSuite,
  269.                                      const IconSelectorValue iconSelector,
  270.                                      const AEIdleUPP idleProcUPP )
  271. {
  272.     OSErr        anErr = noErr;
  273.     
  274.     AppleEvent    theEvent = {typeNull, nil};
  275.     
  276.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  277.                                               kAECoreSuite, kAESetData, &theEvent );
  278.     if ( anErr == noErr )
  279.     {
  280.         AEDesc         itemObject = {typeNull, nil};
  281.         AEDesc        containerObj = { typeNull, nil };        // start with the null (application) container
  282.         
  283.         anErr = AEHMakeAliasObjectFromFSSpec( fssPtr, &containerObj, &itemObject );
  284.         
  285.         if ( anErr == noErr )
  286.         {
  287.             AEDesc         propertyObject = {typeNull, nil};
  288.             
  289.             anErr = AEHMakePropertyObject( pIconBitmap, &itemObject, &propertyObject );    // disposes of itemObject for us
  290.             
  291.             if ( anErr == noErr )
  292.             {
  293.                 anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
  294.                 AEDisposeDesc( &propertyObject );
  295.                 
  296.                 if ( anErr == noErr )
  297.                 {
  298.                     AEDescList    iconFamilyRec = { typeNull, nil };
  299.                     
  300.                     anErr = AEHMakeIconFamilyRecord( theIconSuite, iconSelector, &iconFamilyRec );
  301.  
  302.                     if ( anErr == noErr )
  303.                     {
  304.                         anErr = AEPutParamDesc( &theEvent, keyAEData, &iconFamilyRec );
  305.                         
  306.                         if ( anErr == noErr )
  307.                         {
  308.                             AppleEvent    theReply = {typeNull, nil};
  309.                             AESendMode    sendMode;
  310.                             
  311.                             if ( idleProcUPP == nil )
  312.                                 sendMode = kAENoReply;
  313.                             else
  314.                                 sendMode = kAEWaitReply;
  315.  
  316.                             anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  317.                                             kNoTimeOut, idleProcUPP, nil );
  318.                             
  319.                             AEDisposeDesc( &theEvent );
  320.                             
  321.                             if ( anErr == noErr )
  322.                             {
  323.                                 anErr =  AEHGetHandlerError( &theReply );
  324.                                 
  325.                                 AEDisposeDesc( &theReply );
  326.                             }
  327.                         }
  328.                     }
  329.                 }
  330.             }
  331.         }
  332.     }
  333.     
  334.     return anErr;
  335.     
  336. } // MFEAddCustomIconToItem
  337.  
  338. // *****************************************************************************
  339.  
  340. pascal OSErr MFEGetItemIcon( const FSSpecPtr fssPtr,
  341.                              const AEIdleUPP idleProcUPP,
  342.                                    Handle     *theIconSuite )
  343. {
  344.     OSErr    anErr = noErr;
  345.     
  346.     AppleEvent    theEvent = {typeNull, nil};
  347.     
  348.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  349.                                               kAECoreSuite, kAEGetData, &theEvent );
  350.     if ( anErr == noErr )
  351.     {
  352.         AEDesc         itemObject = {typeNull, nil};
  353.         AEDesc        containerObj = { typeNull, nil };        // start with the null (application) container
  354.         
  355.         anErr = AEHMakeAliasObjectFromFSSpec( fssPtr, &containerObj, &itemObject );
  356.         
  357.         if ( anErr == noErr )
  358.         {
  359.             AEDesc         propertyObject = {typeNull, nil};
  360.             
  361.             anErr = AEHMakePropertyObject( pIconBitmap, &itemObject, &propertyObject );    // disposes of itemObject for us
  362.             
  363.             if ( anErr == noErr )
  364.             {
  365.                 anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
  366.                 AEDisposeDesc( &propertyObject );
  367.                 
  368.                 if ( anErr == noErr )
  369.                 {
  370.                     AppleEvent    theReply = {typeNull, nil};
  371.                     AESendMode    sendMode;
  372.                     
  373.                     if ( idleProcUPP == nil )
  374.                         sendMode = kAENoReply;
  375.                     else
  376.                         sendMode = kAEWaitReply;
  377.  
  378.                     anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  379.                                     kNoTimeOut, idleProcUPP, nil );
  380.                     
  381.                     AEDisposeDesc( &theEvent );
  382.                     
  383.                     if ( anErr == noErr )
  384.                     {
  385.                         anErr =  AEHGetHandlerError( &theReply );
  386.                         
  387.                         if ( anErr == noErr )
  388.                         {
  389.                             AEDesc    iconFamilyRec = { typeNull, nil };
  390.                             
  391.                             anErr = AEGetParamDesc( &theReply, keyDirectObject, typeWildCard, &iconFamilyRec );
  392.                             AEDisposeDesc( &theReply );
  393.                             
  394.                             if ( anErr == noErr )
  395.                             {
  396.                                 anErr = AEHMakeIconSuite( &iconFamilyRec, theIconSuite );
  397.                             }
  398.                             AEDisposeDesc( &iconFamilyRec );
  399.                         }
  400.                     }
  401.                 }
  402.             }
  403.         }
  404.     }
  405.     
  406.     return anErr;
  407.     
  408. } // MFEGetItemIcon
  409.  
  410. // *****************************************************************************
  411.  
  412. pascal OSErr MFEGetEveryItemOnDesktop( const AEIdleUPP idleProcUPP,
  413.                                              AEDescList *objectList )
  414. {
  415.     OSErr        anErr = noErr;
  416.     
  417.     AppleEvent    theEvent = {typeNull, nil};
  418.     
  419.     objectList->descriptorType = typeNull;
  420.     objectList->dataHandle = nil;
  421.     
  422.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  423.                                               kAECoreSuite, kAEGetData, &theEvent );
  424.     if ( anErr == noErr )
  425.     {
  426.         
  427.         AEDesc        containerObj = { typeNull, nil };        // start with the null (application) container
  428.         AEDesc         propertyObject = {typeNull, nil};
  429.         
  430.         anErr = AEHMakePropertyObject( kDesktopFolderType, &containerObj, &propertyObject );
  431.         
  432.         if ( anErr == noErr )
  433.         {
  434.             DescType    selectAll = kAEAll;
  435.             AEDesc        allObjectsDesc = { typeNull, nil };
  436.             
  437.             anErr = AEHMakeSelectionObject( selectAll, &propertyObject, &allObjectsDesc );
  438.             if ( anErr == noErr )
  439.             {
  440.                 anErr = AEPutParamDesc( &theEvent, keyDirectObject, &allObjectsDesc );
  441.                 AEDisposeDesc( &allObjectsDesc );
  442.                 if ( anErr == noErr )
  443.                 {
  444.                     AppleEvent    theReply = {typeNull, nil};
  445.                     AESendMode    sendMode;
  446.                     
  447.                     if ( idleProcUPP == nil )
  448.                         sendMode = kAENoReply;
  449.                     else
  450.                         sendMode = kAEWaitReply;
  451.  
  452.                     anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  453.                                     kNoTimeOut, idleProcUPP, nil );
  454.                     
  455.                     AEDisposeDesc( &theEvent );
  456.                     
  457.                     if ( anErr == noErr )
  458.                     {
  459.                         anErr =  AEHGetHandlerError( &theReply );
  460.                         
  461.                         if ( !anErr && theReply.descriptorType != typeNull )
  462.                         {
  463.                             anErr = AEGetParamDesc( &theReply, keyDirectObject, typeAEList, objectList );
  464.                         }
  465.                         AEDisposeDesc( &theReply );
  466.                     }
  467.                 }
  468.             }
  469.         }
  470.     }
  471.     
  472.     return anErr;
  473.     
  474. } // MFEGetEveryItemOnDesktop
  475.  
  476. // *****************************************************************************
  477.  
  478. pascal OSErr MFEUpdateItemFSS( const FSSpecPtr fssPtr )
  479. {
  480.     OSErr            anErr = noErr;
  481.     AliasHandle        aliasHandle;
  482.     
  483.     anErr = NewAlias( nil, fssPtr, &aliasHandle);
  484.  
  485.     if ( anErr == noErr )
  486.     {
  487.         anErr = MFEUpdateItemAlias( aliasHandle );
  488.     }
  489.     
  490.     DisposeHandle( (Handle)aliasHandle );
  491.     
  492.     return anErr;
  493. } // MFEChangeFolderView
  494.  
  495. // *****************************************************************************
  496.  
  497. pascal OSErr MFEUpdateItemAlias( const AliasHandle aliasHandle )
  498. {
  499.     OSErr        anErr = noErr;
  500.     
  501.     AppleEvent    theEvent = {typeNull, nil};
  502.     
  503.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  504.                                               kAEFinderSuite, kAEUpdate, &theEvent );
  505.     if ( anErr == noErr )
  506.     {
  507.         AEDesc         aliasDesc = {typeNull, nil};
  508.         
  509.         anErr = AEHMakeAliasDesc( aliasHandle, &aliasDesc );
  510.         if ( anErr == noErr )
  511.         {
  512.             anErr = AEPutParamDesc( &theEvent, keyDirectObject, &aliasDesc );
  513.             AEDisposeDesc( &aliasDesc );
  514.             if ( anErr == noErr )
  515.             {
  516.                 AppleEvent    theReply = {typeNull, nil};
  517.                 
  518.                 anErr = AESend( &theEvent, &theReply, kAENoReply, kAENormalPriority,
  519.                                 kAEDefaultTimeout, nil, nil );
  520.                 AEDisposeDesc( &theEvent );
  521.                 
  522.                 if ( anErr == noErr )
  523.                 {
  524.                     anErr =  AEHGetHandlerError( &theReply );
  525.                     
  526.                     AEDisposeDesc( &theReply );
  527.                 }
  528.             }
  529.         }
  530.     }
  531.     
  532.     return anErr;
  533.     
  534. } // MFEChangeFolderView
  535.  
  536. // *****************************************************************************
  537.  
  538. pascal OSErr MFEOpenFile( const AEIdleUPP idleProcUPP,
  539.                                 FSSpec *fssPtr )
  540. {
  541.     OSErr        anErr = noErr;
  542.     
  543.     AppleEvent    theEvent = { typeNull, nil };
  544.     
  545.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  546.                                               kAECoreSuite, kAEOpen, &theEvent );
  547.     if ( anErr == noErr )
  548.     {
  549.         anErr = AEPutParamPtr( &theEvent, keyDirectObject, typeFSS, fssPtr, sizeof( FSSpec ) );
  550.         
  551.         if ( anErr == noErr )
  552.         {    
  553.             AppleEvent    theReply = {typeNull, nil};
  554.             AESendMode    sendMode;
  555.             
  556.             if ( idleProcUPP == nil )
  557.                 sendMode = kAENoReply;
  558.             else
  559.                 sendMode = kAEWaitReply;
  560.  
  561.             anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  562.                             kNoTimeOut, idleProcUPP, nil );
  563.             
  564.             AEDisposeDesc( &theEvent );
  565.             
  566.             if ( anErr == noErr )
  567.             {        
  568.                 anErr =  AEHGetHandlerError( &theReply );
  569.                 AEDisposeDesc( &theReply );
  570.             }
  571.         }
  572.     }
  573.     
  574.     return anErr;
  575. }//end MFELaunchApplication
  576.  
  577.  
  578.